﻿namespace _01_Tower2._0
{
    internal class Game
    {
        public enum Scene
        {
            Attack = 0,
            MainMenu,
            Map1,
            ToolMenu
        }

        bool isOver, isPause;
        long lastTime, fixedTime, currentTime;
        int speed, fps;
        Random rand;
        MapOld map;
        CombatSystem cs;
        ToolMenu toolMenu;
        public Scene scene;
        static public Hero hero;
        static public Enemy slime;
        static public Enemy slime2;
        static public Enemy slime3;
        static public Enemy slime4;
        static public Enemy slime5;
        static public Enemy slime6;

        public Game()
        {
            toolMenu = new ToolMenu(84, 12, 12, 15, 10);
            rand = new Random();
            map = new MapOld();
            cs = new CombatSystem(this);
            hero = new Hero("你",100,100,20,20);
            slime = new Enemy("史莱姆", 10, 30, 21, 15);
            slime2 = new Enemy("史莱姆2", 20, 30, 22, 15);
            slime3 = new Enemy("史莱姆3", 40, 30, 23, 15);
            slime4 = new Enemy("史莱姆4", 60, 30, 24, 15);
            slime5 = new Enemy("史莱姆5", 80, 30, 25, 15);
            slime6 = new Enemy("史莱姆6", 100, 30, 26, 15);
        }

        public void Play()
        {
            Init();
            fixedTime = DateTime.Now.Ticks;
            lastTime = DateTime.Now.Ticks;
            while (!isOver)
            {
                InputSystem.Instance.Update();
                Input();
                // 游戏的暂停其实是逻辑的暂停 按键检测和画面更新不能停
                if (!isPause)
                {
                    Update();
                }
                currentTime = DateTime.Now.Ticks;
                if (currentTime - fixedTime > 10_000_000 / speed)
                {
                    if (!isPause)
                    {
                        FixedUpdate();
                    }
                    fixedTime = DateTime.Now.Ticks;
                }
                if (currentTime - lastTime > 10_000_000 / fps)
                {
                    Render();
                    lastTime = DateTime.Now.Ticks;
                }
            }
            End();
        }

        void Init()
        {
            Console.CursorVisible = false;
            isOver = false;
            isPause = false;
            speed = 10;
            fps = 30;
            scene = Scene.Map1;

            map.Init();
            hero.Init(map,cs,toolMenu,this);
            
        }

        void Input()
        {
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.P))
            {
                isPause = !isPause;
            }
            //hero.Input();
            //toolMenu.Input();
            switch (scene)
            {
                case Scene.Map1:
                    hero.Input();
                    if (InputSystem.Instance.IsKeyPressed(ConsoleKey.O))
                    {
                        scene = Scene.ToolMenu;
                        toolMenu.isOpen = true;
                        break;
                    }
                    break;
                case Scene.ToolMenu:
                    toolMenu.Input();
                    if (InputSystem.Instance.IsKeyPressed(ConsoleKey.O))
                    {
                        scene = Scene.Map1;
                        toolMenu.isOpen = false;
                        break;
                    }
                    break;
                case Scene.Attack:
                    cs.Input();
                    break;
            }
        }

        void Update()
        {
            cs.Update();
            hero.Update();
            toolMenu.Update();
            toolMenu.PrintInfo();
        }
        void FixedUpdate()
        {

        }

        void Render()
        {
            InfoSystem.Instance.PrintInfo();
            //toolMenu.PrintInfo();
            map.Render();
            hero.Render();
            cs.Render();
        }

        void End()
        {

        }

        
    }
}
